<!DOCTYPE HTML>
<html class="sidebar-visible no-js light" lang="zh-CN">
<head>
    <!-- Book generated using mdBook -->
    <meta charset="UTF-8">
    <title>指针和引用 - 通过例子学 Rust</title>


    <!-- Custom HTML head -->


    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
    <meta content="通过例子学 Rust, Rust By Example 中文版，RBE 中文版" name="description">
    <meta content="width=device-width, initial-scale=1" name="viewport">
    <meta content="#ffffff" name="theme-color"/>

    <link href="../../../favicon.svg" rel="icon">
    <link href="../../../favicon.png" rel="shortcut icon">
    <link href="../../../css/variables.css" rel="stylesheet">
    <link href="../../../css/general.css" rel="stylesheet">
    <link href="../../../css/chrome.css" rel="stylesheet">
    <link href="../../../css/print.css" media="print" rel="stylesheet">

    <!-- Fonts -->
    <link href="../../../FontAwesome/css/font-awesome.css" rel="stylesheet">

    <link href="../../../fonts/fonts.css" rel="stylesheet">


    <!-- Highlight.js Stylesheets -->
    <link href="../../../highlight.css" rel="stylesheet">
    <link href="../../../tomorrow-night.css" rel="stylesheet">
    <link href="../../../ayu-highlight.css" rel="stylesheet">

    <!-- Custom theme stylesheets -->


</head>
<body>
<!-- Provide site root to javascript -->
<script type="text/javascript">
    var path_to_root = "../../../";
    var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
</script>

<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script type="text/javascript">
    try {
        var theme = localStorage.getItem('mdbook-theme');
        var sidebar = localStorage.getItem('mdbook-sidebar');

        if (theme.startsWith('"') && theme.endsWith('"')) {
            localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
        }

        if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
            localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
        }
    } catch (e) {
    }
</script>

<!-- Set the theme before any content is loaded, prevents flash -->
<script type="text/javascript">
    var theme;
    try {
        theme = localStorage.getItem('mdbook-theme');
    } catch (e) {
    }
    if (theme === null || theme === undefined) {
        theme = default_theme;
    }
    var html = document.querySelector('html');
    html.classList.remove('no-js')
    html.classList.remove('light')
    html.classList.add(theme);
    html.classList.add('js');
</script>

<!-- Hide / unhide sidebar before it is displayed -->
<script type="text/javascript">
    var html = document.querySelector('html');
    var sidebar = 'hidden';
    if (document.body.clientWidth >= 1080) {
        try {
            sidebar = localStorage.getItem('mdbook-sidebar');
        } catch (e) {
        }
        sidebar = sidebar || 'visible';
    }
    html.classList.remove('sidebar-visible');
    html.classList.add("sidebar-" + sidebar);
</script>

<nav aria-label="Table of contents" class="sidebar" id="sidebar">
    <div class="sidebar-scrollbox">
        <ol class="chapter">
            <li class="chapter-item expanded affix "><a href="../../../index.html">简介</a></li>
            <li class="chapter-item expanded "><a href="../../../hello.html"><strong aria-hidden="true">1.</strong>
                Hello World</a></li>
            <li>
                <ol class="section">
                    <li class="chapter-item expanded "><a href="../../../hello/comment.html"><strong aria-hidden="true">1.1.</strong>
                        注释</a></li>
                    <li class="chapter-item expanded "><a href="../../../hello/print.html"><strong aria-hidden="true">1.2.</strong>
                        格式化输出</a></li>
                    <li>
                        <ol class="section">
                            <li class="chapter-item expanded "><a href="../../../hello/print/print_debug.html"><strong
                                    aria-hidden="true">1.2.1.</strong> 调试（debug）</a></li>
                            <li class="chapter-item expanded "><a href="../../../hello/print/print_display.html"><strong
                                    aria-hidden="true">1.2.2.</strong> 显示（display）</a></li>
                            <li>
                                <ol class="section">
                                    <li class="chapter-item expanded "><a
                                            href="../../../hello/print/print_display/testcase_list.html"><strong
                                            aria-hidden="true">1.2.2.1.</strong> 测试实例：List</a></li>
                                </ol>
                            </li>
                            <li class="chapter-item expanded "><a href="../../../hello/print/fmt.html"><strong
                                    aria-hidden="true">1.2.3.</strong> 格式化</a></li>
                        </ol>
                    </li>
                </ol>
            </li>
            <li class="chapter-item expanded "><a href="../../../primitives.html"><strong aria-hidden="true">2.</strong>
                原生类型</a></li>
            <li>
                <ol class="section">
                    <li class="chapter-item expanded "><a href="../../../primitives/literals.html"><strong
                            aria-hidden="true">2.1.</strong> 字面量和运算符</a></li>
                    <li class="chapter-item expanded "><a href="../../../primitives/tuples.html"><strong
                            aria-hidden="true">2.2.</strong> 元组</a></li>
                    <li class="chapter-item expanded "><a href="../../../primitives/array.html"><strong
                            aria-hidden="true">2.3.</strong> 数组和切片</a></li>
                </ol>
            </li>
            <li class="chapter-item expanded "><a href="../../../custom_types.html"><strong
                    aria-hidden="true">3.</strong> 自定义类型</a></li>
            <li>
                <ol class="section">
                    <li class="chapter-item expanded "><a href="../../../custom_types/structs.html"><strong
                            aria-hidden="true">3.1.</strong> 结构体</a></li>
                    <li class="chapter-item expanded "><a href="../../../custom_types/enum.html"><strong
                            aria-hidden="true">3.2.</strong> 枚举</a></li>
                    <li>
                        <ol class="section">
                            <li class="chapter-item expanded "><a
                                    href="../../../custom_types/enum/enum_use.html"><strong
                                    aria-hidden="true">3.2.1.</strong> 使用 use</a></li>
                            <li class="chapter-item expanded "><a href="../../../custom_types/enum/c_like.html"><strong
                                    aria-hidden="true">3.2.2.</strong> C 风格用法</a></li>
                            <li class="chapter-item expanded "><a
                                    href="../../../custom_types/enum/testcase_linked_list.html"><strong
                                    aria-hidden="true">3.2.3.</strong> 测试实例：链表</a></li>
                        </ol>
                    </li>
                    <li class="chapter-item expanded "><a href="../../../custom_types/constants.html"><strong
                            aria-hidden="true">3.3.</strong> 常量</a></li>
                </ol>
            </li>
            <li class="chapter-item expanded "><a href="../../../variable_bindings.html"><strong
                    aria-hidden="true">4.</strong> 变量绑定</a></li>
            <li>
                <ol class="section">
                    <li class="chapter-item expanded "><a href="../../../variable_bindings/mut.html"><strong
                            aria-hidden="true">4.1.</strong> 可变变量</a></li>
                    <li class="chapter-item expanded "><a href="../../../variable_bindings/scope.html"><strong
                            aria-hidden="true">4.2.</strong> 作用域和遮蔽</a></li>
                    <li class="chapter-item expanded "><a href="../../../variable_bindings/declare.html"><strong
                            aria-hidden="true">4.3.</strong> 变量先声明</a></li>
                </ol>
            </li>
            <li class="chapter-item expanded "><a href="../../../types.html"><strong aria-hidden="true">5.</strong> 类型系统</a>
            </li>
            <li>
                <ol class="section">
                    <li class="chapter-item expanded "><a href="../../../types/cast.html"><strong aria-hidden="true">5.1.</strong>
                        类型转换</a></li>
                    <li class="chapter-item expanded "><a href="../../../types/literals.html"><strong
                            aria-hidden="true">5.2.</strong> 字面量</a></li>
                    <li class="chapter-item expanded "><a href="../../../types/inference.html"><strong
                            aria-hidden="true">5.3.</strong> 类型推断</a></li>
                    <li class="chapter-item expanded "><a href="../../../types/alias.html"><strong aria-hidden="true">5.4.</strong>
                        别名</a></li>
                </ol>
            </li>
            <li class="chapter-item expanded "><a href="../../../conversion.html"><strong aria-hidden="true">6.</strong>
                类型转换</a></li>
            <li>
                <ol class="section">
                    <li class="chapter-item expanded "><a href="../../../conversion/from_into.html"><strong
                            aria-hidden="true">6.1.</strong> From 和 Into</a></li>
                    <li class="chapter-item expanded "><a href="../../../conversion/try_from_try_into.html"><strong
                            aria-hidden="true">6.2.</strong> TryFrom 和 TryInto</a></li>
                    <li class="chapter-item expanded "><a href="../../../conversion/string.html"><strong
                            aria-hidden="true">6.3.</strong> ToString 和 FromStr</a></li>
                </ol>
            </li>
            <li class="chapter-item expanded "><a href="../../../expression.html"><strong aria-hidden="true">7.</strong>
                表达式</a></li>
            <li class="chapter-item expanded "><a href="../../../flow_control.html"><strong
                    aria-hidden="true">8.</strong> 流程控制</a></li>
            <li>
                <ol class="section">
                    <li class="chapter-item expanded "><a href="../../if_else.html"><strong
                            aria-hidden="true">8.1.</strong> if/else</a></li>
                    <li class="chapter-item expanded "><a href="../../loop.html"><strong
                            aria-hidden="true">8.2.</strong> loop 循环</a></li>
                    <li>
                        <ol class="section">
                            <li class="chapter-item expanded "><a href="../../loop/nested.html"><strong
                                    aria-hidden="true">8.2.1.</strong> 嵌套循环和标签</a></li>
                            <li class="chapter-item expanded "><a href="../../loop/return.html"><strong
                                    aria-hidden="true">8.2.2.</strong> 从 loop 循环返回</a></li>
                        </ol>
                    </li>
                    <li class="chapter-item expanded "><a href="../../while.html"><strong
                            aria-hidden="true">8.3.</strong> while 循环</a></li>
                    <li class="chapter-item expanded "><a href="../../for.html"><strong
                            aria-hidden="true">8.4.</strong> for 循环和区间</a></li>
                    <li class="chapter-item expanded "><a href="../../match.html"><strong
                            aria-hidden="true">8.5.</strong> match 匹配</a></li>
                    <li>
                        <ol class="section">
                            <li class="chapter-item expanded "><a href="../destructuring.html"><strong
                                    aria-hidden="true">8.5.1.</strong> 解构</a></li>
                            <li>
                                <ol class="section">
                                    <li class="chapter-item expanded "><a
                                            href="destructure_tuple.html"><strong
                                            aria-hidden="true">8.5.1.1.</strong> 元组</a></li>
                                    <li class="chapter-item expanded "><a
                                            href="destructure_enum.html"><strong
                                            aria-hidden="true">8.5.1.2.</strong> 枚举</a></li>
                                    <li class="chapter-item expanded "><a class="active"
                                                                          href="../../../flow_control/match/destructuring/destructure_pointers.html"><strong
                                            aria-hidden="true">8.5.1.3.</strong> 指针和引用</a></li>
                                    <li class="chapter-item expanded "><a
                                            href="destructure_structures.html"><strong
                                            aria-hidden="true">8.5.1.4.</strong> 结构体</a></li>
                                </ol>
                            </li>
                            <li class="chapter-item expanded "><a href="../guard.html"><strong
                                    aria-hidden="true">8.5.2.</strong> 卫语句</a></li>
                            <li class="chapter-item expanded "><a
                                    href="../binding.html"><strong
                                    aria-hidden="true">8.5.3.</strong> 绑定</a></li>
                        </ol>
                    </li>
                    <li class="chapter-item expanded "><a href="../../if_let.html"><strong
                            aria-hidden="true">8.6.</strong> if let</a></li>
                    <li class="chapter-item expanded "><a href="../../while_let.html"><strong
                            aria-hidden="true">8.7.</strong> while let</a></li>
                </ol>
            </li>
            <li class="chapter-item expanded "><a href="../../../fn.html"><strong aria-hidden="true">9.</strong> 函数</a>
            </li>
            <li>
                <ol class="section">
                    <li class="chapter-item expanded "><a href="../../../fn/methods.html"><strong aria-hidden="true">9.1.</strong>
                        方法</a></li>
                    <li class="chapter-item expanded "><a href="../../../fn/closures.html"><strong aria-hidden="true">9.2.</strong>
                        闭包</a></li>
                    <li>
                        <ol class="section">
                            <li class="chapter-item expanded "><a href="../../../fn/closures/capture.html"><strong
                                    aria-hidden="true">9.2.1.</strong> 捕获</a></li>
                            <li class="chapter-item expanded "><a
                                    href="../../../fn/closures/input_parameters.html"><strong
                                    aria-hidden="true">9.2.2.</strong> 作为输入参数</a></li>
                            <li class="chapter-item expanded "><a href="../../../fn/closures/anonymity.html"><strong
                                    aria-hidden="true">9.2.3.</strong> 类型匿名</a></li>
                            <li class="chapter-item expanded "><a
                                    href="../../../fn/closures/input_functions.html"><strong
                                    aria-hidden="true">9.2.4.</strong> 输入函数</a></li>
                            <li class="chapter-item expanded "><a
                                    href="../../../fn/closures/output_parameters.html"><strong
                                    aria-hidden="true">9.2.5.</strong> 作为输出参数</a></li>
                            <li class="chapter-item expanded "><a
                                    href="../../../fn/closures/closure_examples.html"><strong
                                    aria-hidden="true">9.2.6.</strong> std 中的例子</a></li>
                            <li>
                                <ol class="section">
                                    <li class="chapter-item expanded "><a
                                            href="../../../fn/closures/closure_examples/iter_any.html"><strong
                                            aria-hidden="true">9.2.6.1.</strong> Iterator::any</a></li>
                                    <li class="chapter-item expanded "><a
                                            href="../../../fn/closures/closure_examples/iter_find.html"><strong
                                            aria-hidden="true">9.2.6.2.</strong> Iterator::find</a></li>
                                </ol>
                            </li>
                        </ol>
                    </li>
                    <li class="chapter-item expanded "><a href="../../../fn/hof.html"><strong
                            aria-hidden="true">9.3.</strong> 高阶函数</a></li>
                    <li class="chapter-item expanded "><a href="../../../fn/diverging.html"><strong aria-hidden="true">9.4.</strong>
                        发散函数</a></li>
                </ol>
            </li>
            <li class="chapter-item expanded "><a href="../../../mod.html"><strong aria-hidden="true">10.</strong>
                模块</a></li>
            <li>
                <ol class="section">
                    <li class="chapter-item expanded "><a href="../../../mod/visibility.html"><strong
                            aria-hidden="true">10.1.</strong> 可见性</a></li>
                    <li class="chapter-item expanded "><a href="../../../mod/struct_visibility.html"><strong
                            aria-hidden="true">10.2.</strong> 结构体的可见性</a></li>
                    <li class="chapter-item expanded "><a href="../../../mod/use.html"><strong
                            aria-hidden="true">10.3.</strong> use 声明</a></li>
                    <li class="chapter-item expanded "><a href="../../../mod/super.html"><strong aria-hidden="true">10.4.</strong>
                        super 和 self</a></li>
                    <li class="chapter-item expanded "><a href="../../../mod/split.html"><strong aria-hidden="true">10.5.</strong>
                        文件分层</a></li>
                </ol>
            </li>
            <li class="chapter-item expanded "><a href="../../../crates.html"><strong aria-hidden="true">11.</strong>
                crate</a></li>
            <li>
                <ol class="section">
                    <li class="chapter-item expanded "><a href="../../../crates/lib.html"><strong aria-hidden="true">11.1.</strong>
                        库</a></li>
                    <li class="chapter-item expanded "><a href="../../../crates/link.html"><strong aria-hidden="true">11.2.</strong>
                        extern crate</a></li>
                </ol>
            </li>
            <li class="chapter-item expanded "><a href="../../../cargo.html"><strong aria-hidden="true">12.</strong>
                cargo</a></li>
            <li>
                <ol class="section">
                    <li class="chapter-item expanded "><a href="../../../cargo/deps.html"><strong aria-hidden="true">12.1.</strong>
                        依赖</a></li>
                    <li class="chapter-item expanded "><a href="../../../cargo/conventions.html"><strong
                            aria-hidden="true">12.2.</strong> 约定规范</a></li>
                    <li class="chapter-item expanded "><a href="../../../cargo/test.html"><strong aria-hidden="true">12.3.</strong>
                        测试</a></li>
                    <li class="chapter-item expanded "><a href="../../../cargo/build_scripts.html"><strong
                            aria-hidden="true">12.4.</strong> 构建脚本</a></li>
                </ol>
            </li>
            <li class="chapter-item expanded "><a href="../../../attribute.html"><strong aria-hidden="true">13.</strong>
                属性</a></li>
            <li>
                <ol class="section">
                    <li class="chapter-item expanded "><a href="../../../attribute/unused.html"><strong
                            aria-hidden="true">13.1.</strong> 死代码 dead_code</a></li>
                    <li class="chapter-item expanded "><a href="../../../attribute/crate.html"><strong
                            aria-hidden="true">13.2.</strong> crate</a></li>
                    <li class="chapter-item expanded "><a href="../../../attribute/cfg.html"><strong aria-hidden="true">13.3.</strong>
                        cfg</a></li>
                    <li>
                        <ol class="section">
                            <li class="chapter-item expanded "><a href="../../../attribute/cfg/custom.html"><strong
                                    aria-hidden="true">13.3.1.</strong> 自定义条件</a></li>
                        </ol>
                    </li>
                </ol>
            </li>
            <li class="chapter-item expanded "><a href="../../../generics.html"><strong aria-hidden="true">14.</strong>
                泛型</a></li>
            <li>
                <ol class="section">
                    <li class="chapter-item expanded "><a href="../../../generics/gen_fn.html"><strong
                            aria-hidden="true">14.1.</strong> 函数</a></li>
                    <li class="chapter-item expanded "><a href="../../../generics/impl.html"><strong aria-hidden="true">14.2.</strong>
                        实现</a></li>
                    <li class="chapter-item expanded "><a href="../../../generics/gen_trait.html"><strong
                            aria-hidden="true">14.3.</strong> trait</a></li>
                    <li class="chapter-item expanded "><a href="../../../generics/bounds.html"><strong
                            aria-hidden="true">14.4.</strong> 约束</a></li>
                    <li>
                        <ol class="section">
                            <li class="chapter-item expanded "><a
                                    href="../../../generics/bounds/testcase_empty.html"><strong aria-hidden="true">14.4.1.</strong>
                                测试实例：空约束</a></li>
                        </ol>
                    </li>
                    <li class="chapter-item expanded "><a href="../../../generics/multi_bounds.html"><strong
                            aria-hidden="true">14.5.</strong> 多重约束</a></li>
                    <li class="chapter-item expanded "><a href="../../../generics/where.html"><strong
                            aria-hidden="true">14.6.</strong> where 子句</a></li>
                    <li class="chapter-item expanded "><a href="../../../generics/new_types.html"><strong
                            aria-hidden="true">14.7.</strong> newtype 惯用法</a></li>
                    <li class="chapter-item expanded "><a href="../../../generics/assoc_items.html"><strong
                            aria-hidden="true">14.8.</strong> 关联项</a></li>
                    <li>
                        <ol class="section">
                            <li class="chapter-item expanded "><a href="../../../generics/assoc_items/the_problem.html"><strong
                                    aria-hidden="true">14.8.1.</strong> 存在问题</a></li>
                            <li class="chapter-item expanded "><a
                                    href="../../../generics/assoc_items/types.html"><strong
                                    aria-hidden="true">14.8.2.</strong> 关联类型</a></li>
                        </ol>
                    </li>
                    <li class="chapter-item expanded "><a href="../../../generics/phantom.html"><strong
                            aria-hidden="true">14.9.</strong> 虚类型参数</a></li>
                    <li>
                        <ol class="section">
                            <li class="chapter-item expanded "><a
                                    href="../../../generics/phantom/testcase_units.html"><strong aria-hidden="true">14.9.1.</strong>
                                测试实例：单位检查</a></li>
                        </ol>
                    </li>
                </ol>
            </li>
            <li class="chapter-item expanded "><a href="../../../scope.html"><strong aria-hidden="true">15.</strong>
                作用域规则</a></li>
            <li>
                <ol class="section">
                    <li class="chapter-item expanded "><a href="../../../scope/raii.html"><strong aria-hidden="true">15.1.</strong>
                        RAII</a></li>
                    <li class="chapter-item expanded "><a href="../../../scope/move.html"><strong aria-hidden="true">15.2.</strong>
                        所有权和移动</a></li>
                    <li>
                        <ol class="section">
                            <li class="chapter-item expanded "><a href="../../../scope/move/mut.html"><strong
                                    aria-hidden="true">15.2.1.</strong> 可变性</a></li>
                        </ol>
                    </li>
                    <li class="chapter-item expanded "><a href="../../../scope/borrow.html"><strong aria-hidden="true">15.3.</strong>
                        借用</a></li>
                    <li>
                        <ol class="section">
                            <li class="chapter-item expanded "><a href="../../../scope/borrow/mut.html"><strong
                                    aria-hidden="true">15.3.1.</strong> 可变性</a></li>
                            <li class="chapter-item expanded "><a href="../../../scope/borrow/freeze.html"><strong
                                    aria-hidden="true">15.3.2.</strong> 冻结</a></li>
                            <li class="chapter-item expanded "><a href="../../../scope/borrow/alias.html"><strong
                                    aria-hidden="true">15.3.3.</strong> 别名使用</a></li>
                            <li class="chapter-item expanded "><a href="../../../scope/borrow/ref.html"><strong
                                    aria-hidden="true">15.3.4.</strong> ref 模式</a></li>
                        </ol>
                    </li>
                    <li class="chapter-item expanded "><a href="../../../scope/lifetime.html"><strong
                            aria-hidden="true">15.4.</strong> 生命周期</a></li>
                    <li>
                        <ol class="section">
                            <li class="chapter-item expanded "><a href="../../../scope/lifetime/explicit.html"><strong
                                    aria-hidden="true">15.4.1.</strong> 显式标注</a></li>
                            <li class="chapter-item expanded "><a href="../../../scope/lifetime/fn.html"><strong
                                    aria-hidden="true">15.4.2.</strong> 函数</a></li>
                            <li class="chapter-item expanded "><a href="../../../scope/lifetime/methods.html"><strong
                                    aria-hidden="true">15.4.3.</strong> 方法</a></li>
                            <li class="chapter-item expanded "><a href="../../../scope/lifetime/struct.html"><strong
                                    aria-hidden="true">15.4.4.</strong> 结构体</a></li>
                            <li class="chapter-item expanded "><a href="../../../scope/lifetime/trait.html"><strong
                                    aria-hidden="true">15.4.5.</strong> trait</a></li>
                            <li class="chapter-item expanded "><a
                                    href="../../../scope/lifetime/lifetime_bounds.html"><strong aria-hidden="true">15.4.6.</strong>
                                约束</a></li>
                            <li class="chapter-item expanded "><a href="../../../scope/lifetime/lifetime_coercion.html"><strong
                                    aria-hidden="true">15.4.7.</strong> 强制转换</a></li>
                            <li class="chapter-item expanded "><a
                                    href="../../../scope/lifetime/static_lifetime.html"><strong aria-hidden="true">15.4.8.</strong>
                                static</a></li>
                            <li class="chapter-item expanded "><a href="../../../scope/lifetime/elision.html"><strong
                                    aria-hidden="true">15.4.9.</strong> 省略</a></li>
                        </ol>
                    </li>
                </ol>
            </li>
            <li class="chapter-item expanded "><a href="../../../trait.html"><strong aria-hidden="true">16.</strong> 特性
                trait</a></li>
            <li>
                <ol class="section">
                    <li class="chapter-item expanded "><a href="../../../trait/derive.html"><strong aria-hidden="true">16.1.</strong>
                        派生</a></li>
                    <li class="chapter-item expanded "><a href="../../../trait/ops.html"><strong aria-hidden="true">16.2.</strong>
                        运算符重载</a></li>
                    <li class="chapter-item expanded "><a href="../../../trait/drop.html"><strong aria-hidden="true">16.3.</strong>
                        Drop</a></li>
                    <li class="chapter-item expanded "><a href="../../../trait/iter.html"><strong aria-hidden="true">16.4.</strong>
                        Iterators</a></li>
                    <li class="chapter-item expanded "><a href="../../../trait/clone.html"><strong aria-hidden="true">16.5.</strong>
                        Clone</a></li>
                </ol>
            </li>
            <li class="chapter-item expanded "><a href="../../../macros.html"><strong aria-hidden="true">17.</strong> 使用
                macro_rules! 来创建宏</a></li>
            <li>
                <ol class="section">
                    <li class="chapter-item expanded "><a href="../../../macros/syntax.html"><strong aria-hidden="true">17.1.</strong>
                        语法</a></li>
                    <li>
                        <ol class="section">
                            <li class="chapter-item expanded "><a href="../../../macros/designators.html"><strong
                                    aria-hidden="true">17.1.1.</strong> 指示符</a></li>
                            <li class="chapter-item expanded "><a href="../../../macros/overload.html"><strong
                                    aria-hidden="true">17.1.2.</strong> 重载</a></li>
                            <li class="chapter-item expanded "><a href="../../../macros/repeat.html"><strong
                                    aria-hidden="true">17.1.3.</strong> 重复</a></li>
                        </ol>
                    </li>
                    <li class="chapter-item expanded "><a href="../../../macros/dry.html"><strong aria-hidden="true">17.2.</strong>
                        DRY (不写重复代码)</a></li>
                    <li class="chapter-item expanded "><a href="../../../macros/dsl.html"><strong aria-hidden="true">17.3.</strong>
                        DSL (领域专用语言)</a></li>
                    <li class="chapter-item expanded "><a href="../../../macros/variadics.html"><strong
                            aria-hidden="true">17.4.</strong> 可变参数接口</a></li>
                </ol>
            </li>
            <li class="chapter-item expanded "><a href="../../../error.html"><strong aria-hidden="true">18.</strong>
                错误处理</a></li>
            <li>
                <ol class="section">
                    <li class="chapter-item expanded "><a href="../../../error/panic.html"><strong aria-hidden="true">18.1.</strong>
                        panic</a></li>
                    <li class="chapter-item expanded "><a href="../../../error/option_unwrap.html"><strong
                            aria-hidden="true">18.2.</strong> Option 和 unwrap</a></li>
                    <li>
                        <ol class="section">
                            <li class="chapter-item expanded "><a href="../../../error/option_unwrap/map.html"><strong
                                    aria-hidden="true">18.2.1.</strong> 组合算子：map</a></li>
                            <li class="chapter-item expanded "><a
                                    href="../../../error/option_unwrap/and_then.html"><strong
                                    aria-hidden="true">18.2.2.</strong> 组合算子：and_then</a></li>
                        </ol>
                    </li>
                    <li class="chapter-item expanded "><a href="../../../error/result.html"><strong aria-hidden="true">18.3.</strong>
                        结果 Result</a></li>
                    <li>
                        <ol class="section">
                            <li class="chapter-item expanded "><a href="../../../error/result/result_map.html"><strong
                                    aria-hidden="true">18.3.1.</strong> Result 的 map</a></li>
                            <li class="chapter-item expanded "><a href="../../../error/result/result_alias.html"><strong
                                    aria-hidden="true">18.3.2.</strong> 给 Result 取别名</a></li>
                            <li class="chapter-item expanded "><a
                                    href="../../../error/result/early_returns.html"><strong
                                    aria-hidden="true">18.3.3.</strong> 提前返回</a></li>
                            <li class="chapter-item expanded "><a href="../../../error/result/enter_question_mark.html"><strong
                                    aria-hidden="true">18.3.4.</strong> 引入 ?</a></li>
                        </ol>
                    </li>
                    <li class="chapter-item expanded "><a href="../../../error/multiple_error_types.html"><strong
                            aria-hidden="true">18.4.</strong> 处理多种错误类型</a></li>
                    <li>
                        <ol class="section">
                            <li class="chapter-item expanded "><a
                                    href="../../../error/multiple_error_types/option_result.html"><strong
                                    aria-hidden="true">18.4.1.</strong> 从 Option 中取出 Result</a></li>
                            <li class="chapter-item expanded "><a
                                    href="../../../error/multiple_error_types/define_error_type.html"><strong
                                    aria-hidden="true">18.4.2.</strong> 定义一种错误类型</a></li>
                            <li class="chapter-item expanded "><a
                                    href="../../../error/multiple_error_types/boxing_errors.html"><strong
                                    aria-hidden="true">18.4.3.</strong> 把错误 “装箱”</a></li>
                            <li class="chapter-item expanded "><a
                                    href="../../../error/multiple_error_types/reenter_question_mark.html"><strong
                                    aria-hidden="true">18.4.4.</strong> ? 的其他用法</a></li>
                            <li class="chapter-item expanded "><a
                                    href="../../../error/multiple_error_types/wrap_error.html"><strong
                                    aria-hidden="true">18.4.5.</strong> 包裹错误</a></li>
                        </ol>
                    </li>
                    <li class="chapter-item expanded "><a href="../../../error/iter_result.html"><strong
                            aria-hidden="true">18.5.</strong> 遍历 Result</a></li>
                </ol>
            </li>
            <li class="chapter-item expanded "><a href="../../../std.html"><strong aria-hidden="true">19.</strong> 标准库类型</a>
            </li>
            <li>
                <ol class="section">
                    <li class="chapter-item expanded "><a href="../../../std/box.html"><strong
                            aria-hidden="true">19.1.</strong> 箱子、栈和堆</a></li>
                    <li class="chapter-item expanded "><a href="../../../std/vec.html"><strong
                            aria-hidden="true">19.2.</strong> 动态数组 vector</a></li>
                    <li class="chapter-item expanded "><a href="../../../std/str.html"><strong
                            aria-hidden="true">19.3.</strong> 字符串 String</a></li>
                    <li class="chapter-item expanded "><a href="../../../std/option.html"><strong aria-hidden="true">19.4.</strong>
                        选项 Option</a></li>
                    <li class="chapter-item expanded "><a href="../../../std/result.html"><strong aria-hidden="true">19.5.</strong>
                        结果 Result</a></li>
                    <li>
                        <ol class="section">
                            <li class="chapter-item expanded "><a href="../../../std/result/question_mark.html"><strong
                                    aria-hidden="true">19.5.1.</strong> ? 用法</a></li>
                        </ol>
                    </li>
                    <li class="chapter-item expanded "><a href="../../../std/panic.html"><strong aria-hidden="true">19.6.</strong>
                        panic!</a></li>
                    <li class="chapter-item expanded "><a href="../../../std/hash.html"><strong
                            aria-hidden="true">19.7.</strong> 散列表 HashMap</a></li>
                    <li>
                        <ol class="section">
                            <li class="chapter-item expanded "><a href="../../../std/hash/alt_key_types.html"><strong
                                    aria-hidden="true">19.7.1.</strong> 更改或自定义关键字类型</a></li>
                            <li class="chapter-item expanded "><a href="../../../std/hash/hashset.html"><strong
                                    aria-hidden="true">19.7.2.</strong> 散列集 HashSet</a></li>
                        </ol>
                    </li>
                </ol>
            </li>
            <li class="chapter-item expanded "><a href="../../../std_misc.html"><strong aria-hidden="true">20.</strong>
                标准库更多介绍</a></li>
            <li>
                <ol class="section">
                    <li class="chapter-item expanded "><a href="../../../std_misc/threads.html"><strong
                            aria-hidden="true">20.1.</strong> 线程</a></li>
                    <li>
                        <ol class="section">
                            <li class="chapter-item expanded "><a
                                    href="../../../std_misc/threads/testcase_mapreduce.html"><strong aria-hidden="true">20.1.1.</strong>
                                测试实例：map-reduce</a></li>
                        </ol>
                    </li>
                    <li class="chapter-item expanded "><a href="../../../std_misc/channels.html"><strong
                            aria-hidden="true">20.2.</strong> 通道</a></li>
                    <li class="chapter-item expanded "><a href="../../../std_misc/path.html"><strong aria-hidden="true">20.3.</strong>
                        路径</a></li>
                    <li class="chapter-item expanded "><a href="../../../std_misc/file.html"><strong aria-hidden="true">20.4.</strong>
                        文件输入输出（I/O）</a></li>
                    <li>
                        <ol class="section">
                            <li class="chapter-item expanded "><a href="../../../std_misc/file/open.html"><strong
                                    aria-hidden="true">20.4.1.</strong> 打开文件 open</a></li>
                            <li class="chapter-item expanded "><a href="../../../std_misc/file/create.html"><strong
                                    aria-hidden="true">20.4.2.</strong> 创建文件 create</a></li>
                            <li class="chapter-item expanded "><a href="../../../std_misc/file/read_lines.html"><strong
                                    aria-hidden="true">20.4.3.</strong> 读取行 read lines</a></li>
                        </ol>
                    </li>
                    <li class="chapter-item expanded "><a href="../../../std_misc/process.html"><strong
                            aria-hidden="true">20.5.</strong> 子进程</a></li>
                    <li>
                        <ol class="section">
                            <li class="chapter-item expanded "><a href="../../../std_misc/process/pipe.html"><strong
                                    aria-hidden="true">20.5.1.</strong> 管道</a></li>
                            <li class="chapter-item expanded "><a href="../../../std_misc/process/wait.html"><strong
                                    aria-hidden="true">20.5.2.</strong> 等待</a></li>
                        </ol>
                    </li>
                    <li class="chapter-item expanded "><a href="../../../std_misc/fs.html"><strong aria-hidden="true">20.6.</strong>
                        文件系统操作</a></li>
                    <li class="chapter-item expanded "><a href="../../../std_misc/arg.html"><strong aria-hidden="true">20.7.</strong>
                        程序参数</a></li>
                    <li>
                        <ol class="section">
                            <li class="chapter-item expanded "><a href="../../../std_misc/arg/matching.html"><strong
                                    aria-hidden="true">20.7.1.</strong> 参数解析</a></li>
                        </ol>
                    </li>
                    <li class="chapter-item expanded "><a href="../../../std_misc/ffi.html"><strong aria-hidden="true">20.8.</strong>
                        外部语言函数接口</a></li>
                </ol>
            </li>
            <li class="chapter-item expanded "><a href="../../../testing.html"><strong aria-hidden="true">21.</strong>
                测试</a></li>
            <li>
                <ol class="section">
                    <li class="chapter-item expanded "><a href="../../../testing/unit_testing.html"><strong
                            aria-hidden="true">21.1.</strong> 单元测试</a></li>
                    <li class="chapter-item expanded "><a href="../../../testing/doc_testing.html"><strong
                            aria-hidden="true">21.2.</strong> 文档测试</a></li>
                    <li class="chapter-item expanded "><a href="../../../testing/integration_testing.html"><strong
                            aria-hidden="true">21.3.</strong> 集成测试</a></li>
                    <li class="chapter-item expanded "><a href="../../../testing/dev_dependencies.html"><strong
                            aria-hidden="true">21.4.</strong> 开发依赖</a></li>
                </ol>
            </li>
            <li class="chapter-item expanded "><a href="../../../unsafe.html"><strong aria-hidden="true">22.</strong>
                不安全操作</a></li>
            <li class="chapter-item expanded "><a href="../../../compatibility.html"><strong
                    aria-hidden="true">23.</strong> 兼容性</a></li>
            <li>
                <ol class="section">
                    <li class="chapter-item expanded "><a href="../../../compatibility/raw_identifiers.html"><strong
                            aria-hidden="true">23.1.</strong> 原始标志符</a></li>
                </ol>
            </li>
            <li class="chapter-item expanded "><a href="../../../meta.html"><strong aria-hidden="true">24.</strong>
                补充</a></li>
            <li>
                <ol class="section">
                    <li class="chapter-item expanded "><a href="../../../meta/doc.html"><strong
                            aria-hidden="true">24.1.</strong> 文档</a></li>
                </ol>
            </li>
        </ol>
    </div>
    <div class="sidebar-resize-handle" id="sidebar-resize-handle"></div>
</nav>

<div class="page-wrapper" id="page-wrapper">

    <div class="page">

        <div id="menu-bar-hover-placeholder"></div>
        <div class="menu-bar sticky bordered" id="menu-bar">
            <div class="left-buttons">
                <button aria-controls="sidebar" aria-label="Toggle Table of Contents" class="icon-button"
                        id="sidebar-toggle" title="Toggle Table of Contents" type="button">
                    <i class="fa fa-bars"></i>
                </button>
                <button aria-controls="theme-list" aria-expanded="false" aria-haspopup="true" aria-label="Change theme"
                        class="icon-button" id="theme-toggle" title="Change theme" type="button">
                    <i class="fa fa-paint-brush"></i>
                </button>
                <ul aria-label="Themes" class="theme-popup" id="theme-list" role="menu">
                    <li role="none">
                        <button class="theme" id="light" role="menuitem">Light (default)</button>
                    </li>
                    <li role="none">
                        <button class="theme" id="rust" role="menuitem">Rust</button>
                    </li>
                    <li role="none">
                        <button class="theme" id="coal" role="menuitem">Coal</button>
                    </li>
                    <li role="none">
                        <button class="theme" id="navy" role="menuitem">Navy</button>
                    </li>
                    <li role="none">
                        <button class="theme" id="ayu" role="menuitem">Ayu</button>
                    </li>
                </ul>

                <button aria-controls="searchbar" aria-expanded="false" aria-keyshortcuts="S"
                        aria-label="Toggle Searchbar" class="icon-button" id="search-toggle"
                        title="Search. (Shortkey: s)" type="button">
                    <i class="fa fa-search"></i>
                </button>

            </div>

            <h1 class="menu-title">通过例子学 Rust</h1>

            <div class="right-buttons">
                <a aria-label="Print this book" href="../../../print.html" title="Print this book">
                    <i class="fa fa-print" id="print-button"></i>
                </a>

            </div>
        </div>


        <div class="hidden" id="search-wrapper">
            <form class="searchbar-outer" id="searchbar-outer">
                <input aria-controls="searchresults-outer" aria-describedby="searchresults-header" id="searchbar"
                       name="search" name="searchbar" placeholder="Search this book ..." type="search">
            </form>
            <div class="searchresults-outer hidden" id="searchresults-outer">
                <div class="searchresults-header" id="searchresults-header"></div>
                <ul id="searchresults">
                </ul>
            </div>
        </div>


        <!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
        <script type="text/javascript">
            document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
            document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
            Array.from(document.querySelectorAll('#sidebar a')).forEach(function (link) {
                link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
            });
        </script>

        <div class="content" id="content">
            <main>
                <h1><a class="header" href="#指针和引用" id="指针和引用">指针和引用</a></h1>
                <p>对指针来说，解构（destructure）和解引用（dereference）要区分开，因为这两者的概念
                    是不同的，和 <code>C</code> 那样的语言用法不一样。</p>
                <ul>
                    <li>解引用使用 <code>*</code></li>
                    <li>解构使用 <code>&amp;</code>、<code>ref</code>、和 <code>ref mut</code></li>
                </ul>
                <pre><pre class="playground"><code class="language-rust editable">fn main() {
    // 获得一个 `i32` 类型的引用。`&amp;` 表示取引用。
    let reference = &amp;4;

    match reference {
        // 如果用 `&amp;val` 这个模式去匹配 `reference`，就相当于做这样的比较：
        // `&amp;i32`（译注：即 `reference` 的类型）
        //    |
        // `&amp;val`（译注：即用于匹配的模式）
        // ^ 我们看到，如果去掉匹配的 `&amp;`，`i32` 应当赋给 `val`。
        // 译注：因此可用 `val` 表示被 `reference` 引用的值 4。
        &amp;val =&gt; println!(&quot;Got a value via destructuring: {:?}&quot;, val),
    }

    // 如果不想用 `&amp;`，需要在匹配前解引用。
    match *reference {
        val =&gt; println!(&quot;Got a value via dereferencing: {:?}&quot;, val),
    }

    // 如果一开始就不用引用，会怎样？ `reference` 是一个 `&amp;` 类型，因为赋值语句
    // 的右边已经是一个引用。但下面这个不是引用，因为右边不是。
    let _not_a_reference = 3;

    // Rust 对这种情况提供了 `ref`。它更改了赋值行为，从而可以对具体值创建引用。
    // 下面这行将得到一个引用。
    let ref _is_a_reference = 3;

    // 相应地，定义两个非引用的变量，通过 `ref` 和 `ref mut` 仍可取得其引用。
    let value = 5;
    let mut mut_value = 6;

    // 使用 `ref` 关键字来创建引用。
    // 译注：下面的 r 是 `&amp;i32` 类型，它像 `i32` 一样可以直接打印，因此用法上
    // 似乎看不出什么区别。但读者可以把 `println!` 中的 `r` 改成 `*r`，仍然能
    // 正常运行。前面例子中的 `println!` 里就不能是 `*val`，因为不能对整数解
    // 引用。
    match value {
        ref r =&gt; println!(&quot;Got a reference to a value: {:?}&quot;, r),
    }

    // 类似地使用 `ref mut`。
    match mut_value {
        ref mut m =&gt; {
            // 已经获得了 `mut_value` 的引用，先要解引用，才能改变它的值。
            *m += 10;
            println!(&quot;We added 10. `mut_value`: {:?}&quot;, m);
        },
    }
}
</code></pre></pre>

            </main>

            <nav aria-label="Page navigation" class="nav-wrapper">
                <!-- Mobile navigation buttons -->

                <a aria-keyshortcuts="Left" aria-label="Previous chapter" class="mobile-nav-chapters previous"
                   href="destructure_enum.html" rel="prev"
                   title="Previous chapter">
                    <i class="fa fa-angle-left"></i>
                </a>


                <a aria-keyshortcuts="Right" aria-label="Next chapter" class="mobile-nav-chapters next"
                   href="destructure_structures.html" rel="next"
                   title="Next chapter">
                    <i class="fa fa-angle-right"></i>
                </a>


                <div style="clear: both"></div>
            </nav>
        </div>
    </div>

    <nav aria-label="Page navigation" class="nav-wide-wrapper">

        <a aria-keyshortcuts="Left" aria-label="Previous chapter" class="nav-chapters previous"
           href="destructure_enum.html" rel="prev" title="Previous chapter">
            <i class="fa fa-angle-left"></i>
        </a>


        <a aria-keyshortcuts="Right" aria-label="Next chapter" class="nav-chapters next"
           href="destructure_structures.html" rel="next" title="Next chapter">
            <i class="fa fa-angle-right"></i>
        </a>

    </nav>

</div>


<!-- Livereload script (if served using the cli tool) -->
<script type="text/javascript">
    var socket = new WebSocket("ws://localhost:3000/__livereload");
    socket.onmessage = function (event) {
        if (event.data === "reload") {
            socket.close();
            location.reload();
        }
    };

    window.onbeforeunload = function () {
        socket.close();
    }
</script>


<script type="text/javascript">
    window.playground_copyable = true;
</script>


<script charset="utf-8" src="../../../ace.js" type="text/javascript"></script>
<script charset="utf-8" src="../../../editor.js" type="text/javascript"></script>
<script charset="utf-8" src="../../../mode-rust.js" type="text/javascript"></script>
<script charset="utf-8" src="../../../theme-dawn.js" type="text/javascript"></script>
<script charset="utf-8" src="../../../theme-tomorrow_night.js" type="text/javascript"></script>


<script charset="utf-8" src="../../../elasticlunr.min.js" type="text/javascript"></script>
<script charset="utf-8" src="../../../mark.min.js" type="text/javascript"></script>
<script charset="utf-8" src="../../../searcher.js" type="text/javascript"></script>


<script charset="utf-8" src="../../../clipboard.min.js" type="text/javascript"></script>
<script charset="utf-8" src="../../../highlight.js" type="text/javascript"></script>
<script charset="utf-8" src="../../../book.js" type="text/javascript"></script>

<!-- Custom JS scripts -->


</body>
</html>
